#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
   regex_t    preg;
   char       *string = "a very simple simple simple string";
   char       *pattern = "\\(sim[a-z]le\\) \\1 \\1";
   int        rc;
   size_t     nmatch = 2;
   regmatch_t pmatch[3];
 
   if (0 != (rc = regcomp(&preg, pattern, 0))) {
      printf("regcomp() failed, returning nonzero (%d)\n", rc);
      exit(EXIT_FAILURE);
   }
 
   if (0 != (rc = regexec(&preg, string, nmatch, pmatch, 0))) {
      printf("Failed to match '%s' with '%s',returning %d.\n",
             string, pattern, rc);
   }
   else {
      printf("With the whole expression, "
             "a matched substring \"%.*s\" is found at position %d to %d.\n",
             pmatch[0].rm_eo - pmatch[0].rm_so, &string[pmatch[0].rm_so],
             pmatch[0].rm_so, pmatch[0].rm_eo - 1);
      printf("With the sub-expression, "
             "a matched substring \"%.*s\" is found at position %d to %d.\n",
             pmatch[1].rm_eo - pmatch[1].rm_so, &string[pmatch[1].rm_so],
             pmatch[1].rm_so, pmatch[1].rm_eo - 1);
   }
   printf("pmatch[0] eo so: %d %d\n", pmatch[0].rm_eo, pmatch[0].rm_so);
   printf("pmatch[1] eo so: %d %d\n", pmatch[1].rm_eo, pmatch[1].rm_so);
   printf("%.*s\n",pmatch[0].rm_eo - pmatch[0].rm_so, &string[pmatch[0].rm_so]);
   printf("%.*s\n",pmatch[1].rm_eo - pmatch[1].rm_so, &string[pmatch[1].rm_so]);
   regfree(&preg);
   return 0;
}